package main

import (
	"gorm.io/gorm"
)

//type Tag struct {
//	ID   uint   `gorm:"type:uint"`
//	Name string `gorm:"type:varchar(20)"`
//	//Article []Brticle `gorm:"many2many:brticle_tag"` //用于反向引用
//}
//type Brticle struct {
//	ID    uint   `gorm:"type:uint"`
//	Title string `gorm:"type:varchar(30)"`
//	Tag   []Tag  `gorm:"many2many:brticle_tags"`
//}

func main() {
	DB.SetupJoinTable(&Brticle{}, "Tag", &BrticleTag{}) //自定义连接表 通过Brticle表查设置tag
	//DB.AutoMigrate(&Tag{}, &Brticle{}, &BrticleTag{})
	DB = DB.Session(&gorm.Session{
		Logger: mysqlLogger,
	})
	/*
		多对多
			//1.1添加文章时一起添加标签
			//DB.Create(&Brticle{
			//	Title: "python基础",
			//	Tag: []Tag{
			//		{
			//			Name: "python",
			//		},
			//		{
			//			Name: "后端",
			//		},
			//	},
			//})
			//1.2创建文章关联已有标签
			//var tagList []Tag
			//DB.Find(&tagList, "name in ?", []string{"后端"})
			//DB.Create(&Brticle{
			//	Title: "golang",
			//	Tag:   tagList,
			//})
			//2.1 查询文章并显示文章的标签列表
			//var brticle Brticle
			//DB.Preload("Tag").Take(&brticle)
			//fmt.Println(brticle)
			//2.2 查询标签并显示对应的文章列表
			//var tag Tag
			//DB.Preload("Brticle").Take(&tag)
			//fmt.Println(tag)
			//3.1多对多的更新
			//先删除原有的  再添加新的
			//var brticle Brticle
			//DB.Preload("Tag").Take(&brticle, 1)
			////fmt.Println(brticle)
			//DB.Model(&brticle).Association("Tag").Delete(brticle.Tag)
			//var tag Tag
			//DB.Take(&tag, 1)
			//DB.Model(&brticle).Association("Tag").Append(&tag)
			//var britcle Brticle
			//DB.Preload("Tag").Take(&britcle, 1)
			//var tag Tag
			//DB.Model(&britcle).Association("Tag").Replace(&tag)
	*/
}
